const express = require('express');
const path = require('path');
const sqlite3 = require('sqlite3');
const cors = require('cors');

let database = new sqlite3.Database(path.join(__dirname, "databases", "main.db"), (err) => {
    if (err) throw err;
});
let send_object = {
    code: 0,

    msg: "操作完成"
}
function init_send_object() {
    send_object = {
        code: 0,
        msg: "操作完成"
    }
}
const app = express();
// 设置 CORS 中间件允许所有来源的跨域请求
app.use(cors({
    origin: '*' // 允许所有来源
}));
app.use(express.json()); // 接收post请求时，发现req.body是undefined。在启动时加上一句app.use(express.json()); 就可以了。



app.post("/get_user_now_class_data", async (req, res) => {
    send_object.data = {
        className:"计算机网络",
        classTime:"02:30",
        classAddress:"一教607"
    }
    res.json(send_object)
})

// data
//  target_name
//  target_time
//  color_theme
//  start_time
app.post("/set_user_target_data", async (req, res) => {
    try {
        //          0|ID|INT|1||1
        //          1|target_name|TEXT|0||0
        //          2|target_time|TEXT|0||0
        //          3|color_theme|TEXT|0||0
        //          4|start_time|TEXT|0||0
        let { data } = req.body
        console.log(data);
        if (data.target_name.length <= 8) {
            let sql = ""
            if (data.target_time.split("-").length == 3) {
                sql = "UPDATE user_target SET target_name = '" + data.target_name + "' WHERE ID = 1"
                console.log(sql);
                database.run(sql)
                sql = "UPDATE user_target SET target_time = '" + data.target_time + "' WHERE ID = 1"
                console.log(sql);
                database.run(sql)
                sql = "UPDATE user_target SET color_theme = \"" + data.color_theme + "\" WHERE ID = 1"
                console.log(sql);
                database.run(sql)
                sql = "UPDATE user_target SET start_time = \"" + data.start_time + "\" WHERE ID = 1"
                console.log(sql);
                database.run(sql)

            } else {
                send_object.code = -1
                send_object.msg = "target_time不符合格式 正确格式例:2024-12-11";
            }
        } else {
            send_object.code = -1
            send_object.msg = "target_name长度过高"
        }
        res.json(send_object)
        init_send_object();
    } catch (e) {

    }
})


app.get('/get_user_target_data', async (req, res) => {
    try {
        // 查询World表中的所有数据
        let user_target = await new Promise((resolve, reject) => {
            database.get("SELECT * FROM user_target", [], (err, rows) => {
                if (err) {
                    console.error(err.message);
                    reject(err);
                } else {
                    resolve(rows);
                }
            });
        });
        send_object.data = {
            ID: user_target.ID,
            color_theme: user_target.color_theme,
            target_time: user_target.target_time,
            target_name: user_target.target_name,
            start_time: user_target.start_time
        }
        res.json(send_object);
        init_send_object()
    } catch (error) {
        res.status(500).json({
            code: -1,
            msg: error.message
        });
    }
});

app.listen(3000, () => {
    console.log("local: http://localhost:3000");
    console.log("network: http://212.64.11.29:3000")
});
